import 'package:flutter/foundation.dart';
import 'package:logger/logger.dart';

class MyLogUtil {
  var logger;
  static MyLogUtil? _instance;

  factory MyLogUtil() => getSingleton()!;

  MyLogUtil._internal() {
    logger = Logger(
      level: Logger.level,
      filter: MyFilter(),
      output: ConsoleOutput(),
      printer: PrettyPrinter(
          noBoxingByDefault: false,
          methodCount: 2,
          errorMethodCount: 2,
          lineLength: 120,
          colors: true,
          printEmojis: true,
          printTime: false),
    );
  }

  static MyLogUtil? getSingleton() {
    _instance ??= MyLogUtil._internal();
    return _instance;
  }

  i(dynamic str) {
    logger.i('${DateTime.now().toIso8601String()}:::$str');
  }

  d(dynamic str) {
    logger.d('${DateTime.now().toIso8601String()}:::$str');
  }

  e(dynamic str) {
    logger.e('${DateTime.now().toIso8601String()}:::$str');
  }

  w(dynamic str) {
    logger.w('${DateTime.now().toIso8601String()}:::$str');
  }

  v(dynamic str) {
    logger.v('${DateTime.now().toIso8601String()}:::$str');
  }
}

class MyFilter extends LogFilter {
  @override
  bool shouldLog(LogEvent event) {
    return true;
  }
}

class ConsoleOutput extends LogOutput {
  @override
  void output(OutputEvent event) {
    for (var line in event.lines) {
      if (kDebugMode) {
        print(line);
      }
    }
  }
}
